package com.example.javabasic.suanfa.jiandan.栈与队列;

/**
 * @description 第k大
 * @Author Lin FuYuan
 * @date 2021/5/19
 */
public class KthLargest {

    int[] arr = null;
    int[] nums = null;
    int n = 0;

    public KthLargest(int k, int[] nums) {
        //初始化一个k大的数组
        arr = new int[k];
        this.nums = nums;
        for (int i = 0; i < nums.length; i++) {
            //如果n<k  则插入数据
            if (nums[i] < arr[n] && n < k) {
                arr[++n] = nums[i];
                continue;
            }
            int x = n;
            while (nums[i] > arr[x] && x >= 0) {
                int now = arr[x];
                if (x < k) {
                    arr[x + 1] = now;
                }
                arr[x] = nums[i];
                x--;
            }
        }
    }

    public int add(int val) {
        return arr[arr.length - 1];
    }
}
